﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.Core;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class ProductListImpl : IProductList
    {
        public bool IsNameDuplicate(ProductListModel model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var cnt = db.ProductLists.Count(c => c.Name == model.Name);
            return cnt > 0 ? true : false;
        }

        public Core.DBUtility.ExecutedResult Add(Models.ProductListModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            model.CreateTime = DateTime.Now;

            var op = model.ToEntity();
            try
            {
                db.ProductLists.AddObject(op);
                db.SaveChanges();

            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ProductListMsg.AddFailed);
                Log.Error("IProductList.Add", ex);
            }

            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ProductListModel model)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();

            try
            {

                var entity = db.ProductLists.SingleOrDefault(l => l.ProductID == model.ProductID);
                if (entity != null)
                {
                    entity.Name = model.Name;
                    entity.Size = model.Size;
                    entity.Suit = model.Suit;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, WarehouseMsg.UpdateFailed);
                Log.Error("IProductList.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.ProductLists.Where(l => ids.Contains(l.ProductID));
            foreach (var e in entity)
            {

                try
                {
                    db.ProductLists.DeleteObject(e);
                }
                catch
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, "无法删除");
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, WarehouseMsg.DeleteFailed);
                    Log.Error("IProductList.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ProductListModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ProductListModel GetModelByID(Guid id)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var mcmp = db.ProductLists.SingleOrDefault(c => c.ProductID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public Models.ProductListModel GetModelByModel(Models.ProductListModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ProductListModel> Query(Models.ProductListModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            IEnumerable<ProductList> entities = from b in db.ProductLists
                                                select b;

            totalCount = entities.Count();
            var pagedEntities = entities.OrderBy(p => p.Name).ToList();

            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();

            return new List<ProductListModel>();
        }

        public List<Models.ProductListModel> Query(Models.ProductListModel Model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            IEnumerable<ProductList> entities = from b in db.ProductLists
                                                select b;
            var pagedEntities = entities.OrderBy(p => p.Name).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<ProductListModel>();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
